<?php

	// +----------------------------------------------------------------------
	// | 前台首页控制器
	// | 主要获取首页聚合数据.
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

namespace app\controller\app;

	use app\event\app\Device as DeviceEvent;
	use app\event\cms\Document as DocumentEvent;
    use app\model\app\Version as VersionModel;
    use app\model\cms\Document as DocumentModel;
	use app\common\event\Fanyi;
	use app\event\store\Order as OrderEvent;
	use app\model\store\Order as OrderModel;
	use app\model\store\Store as StoreModel;
	use app\event\ucenter\User;
	use app\model\ucenter\ActionLog;
	use app\model\ucenter\User as UserModel;
	use extend\RSA;
	use think\facade\Console;
	use think\facade\Db;
	use think\facade\Config;
	use think\facade\Env;
	use think\facade\Event;
	use yicmf\ReceiveMail;

	class Index extends Common
	{
		// 系统首页
		public function index()
		{

            $app = VersionModel::where('status',1)->where('is_silence',0)->where('is_examine',0)->where('platform','android')->order('version_code DESC')->find();
            $this->assign('android_url',$app['pkg_url']);
            $this->assign('ios_url',$app['pkg_url']);
			return $this->fetch();
		}

		public function fy()
		{
			$fy = new Fanyi();

			dump($fy->test());
		}

		public function test()
		{

			$fy = new Fanyi();

			halt($fy->test());
			$mailServer="imap.exmail.qq.com"; //IMAP主机
			$mailLink="{{$mailServer}:143}INBOX" ; //imagp连接地址：不同主机地址不同
			$mailUser = '279657903@qq.comxd'; //邮箱用户名
			$mailPass = 'sowmznawmhetcbci'; //邮箱密码
			$mbox = imap_open($mailLink,$mailUser,$mailPass); //开启信箱imap_open
			$totalrows = imap_num_msg($mbox); //取得信件数
			for ($i=1;$i<$totalrows;$i++){
				$headers = imap_fetchheader($mbox, $i); //获取信件标头
				$headArr = matchMailHead($headers); //匹配信件标头
				$mailBody = imap_fetchbody($mbox, $i, 1); //获取信件正文
			}
			/**
			 *
			 * 匹配提取信件头部信息
			 * @param String $str
			 */
			function matchMailHead($str)
			{
				$headList = array();
				$headArr = array(
					'from',
					'to',
					'date',
					'subject'
				);
				foreach ($headArr as $key) {
					if (preg_match('/' . $key . ':(.*?)[\n\r]/is', $str, $m)) {
						$match = trim($m[1]);
						$headList[$key] = $key == 'date' ? date('Y-m-d H:i:s', strtotime($match)) : $match;
					}
				}
				dump($headList);
			}
				exit();
			$obj= new ReceiveMail('yicmf@sina.cn','e950ebc213499c80','yicmf@sina.cn','pop.sina.cn','pop3',110,false);

			//Connect to the Mail Box
			$res=$obj->connect();     //If connection fails give error message and exit
			if (!$res)
			{
				dump( array("msg"=>"Error: Connecting to mail server"));
			}
			// Get Total Number of Unread Email in mail box
			$tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value
			if($tot < 1) { //如果信件数为0,显示信息
				dump(  array("msg"=>"No Message for "));
			}
			else
			{
				$res=array("msg"=>"Total Mails:: $tot<br>");

				for($i=$tot;$i>0;$i--)
				{
					$head=$obj->getHeaders($i); // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)

					//处理邮件附件
					$files=$obj->GetAttach($i,$this->savePath); // 获取邮件附件，返回的邮件附件信息数组

					$imageList=array();
					foreach($files as $k => $file)
					{
						//type=1为附件,0为邮件内容图片
						if($file['type'] == 0)
						{
							$imageList[$file['title']]=$file['pathname'];
						}
					}
					$body = $obj->getBody($i,$this->webPath,$imageList);

					$res['mail'][]=array('head'=>$head,'body'=>$body,"attachList"=>$files);
//       $obj->deleteMails($i); // Delete Mail from Mail box
//       $obj->move_mails($i,"taskMail");
				}
				$obj->close_mailbox();  //Close Mail Box
				dump($res );
			}
			exit();

			$all[] = ['action_status' => 1, 'ip' => 2, 'action_id' => 3, 'update_time' => time_format(), 'create_time' => time_format(), 'status' => 1];
			$all[] = ['action_status' => 1, 'ip' => 2, 'action_id' => 3, 'update_time' => time_format(), 'create_time' => time_format(), 'status' => 1];
			$all[] = ['action_status' => 1, 'ip' => 2, 'action_id' => 3, 'update_time' => time_format(), 'create_time' => time_format(), 'status' => 1];

			dump(ActionLog::insertAll($all));
			exit();
			halt(array_merge(['d' => 55555, 'ddd' => 12], ['da1' => 3, 'd' => 56]));

			halt(view('store@common/settlement'));
//			halt(time_format(strtotime('today')));

			$stores = StoreModel::where('status', 1)
				->whereTime('end_time', '>', 'now')
				->whereTime('update_time', '>', '-3 day')
				->where('id', 'NOT IN', function ($query) {
					$query->name('bill')->whereTime('create_time', 'today')->where('status', 1)->field('store_id');;
				})
				->select();

//			$stores = StoreModel
//				::where('status', 1)
////			->whereTime('end_time','>','now')
//
////			->whereTime('update_time','>','-3 day')
//				->where('id', 'NOT IN', function ($query) {
//						$query->name('bill')->whereTime('create_time','today')->where('status', 1)->field('store_id');;
//					})
//			->select();
//			$stores = StoreModel
//				::where('status', 1)
//			->whereTime('end_time','>','now')
//				->where('recent_settlement_time', ['=', null], ['<', time_format()], 'or')
////			->whereTime('recent_settlement_time','<','today')
//			->whereTime('update_time','>','-3 day')
//			->select();

			halt($stores);
			$title = time_format(time(), 'Y年m月d日') . '结算公告';
			$param = [];
			$param['title'] = $title;
			$param['content'] = '<p><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><strong>尊敬的商户您好：</strong></span></span></p>
<p><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><strong>自动提现：</strong></span></span></p>
<p><span style="color:#3498db;"><font face="Microsoft YaHei"><span style="font-size:14px"><b>收款方式</b></span></font><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><strong>为支付宝、微信的商户系统已结算完毕！请注意查看您的支付宝账单！</strong></span></span></span></p>
<p><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><strong>手动提现：</strong></span></span></p>
<p><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><strong>提现后请耐心等待我们财务进行打款！</strong></span></span></p>
<p style="text-align: right;"><strong><span style="font-size:14px;"><span style="font-family:Microsoft YaHei;">'
				. Config::get('setting.web_site_title') . '财务</span></span></strong></p>';
			$param['pattern_id'] = 7;
			$param['category_id'] = 3;
			$param['status'] = 1;
			$user = UserModel::where('id', Config::get('ucenter.system_notice_from_user_id'))->find();
			$document = new DocumentEvent();
			$re = $document->update($param, $user);


			halt($re);
			$data = Event::trigger('send_sms', 11);
			halt($data);

			halt(PHP_SAPI);
			dump(think_decrypt('MDAwMDAwMDAwMH6LeauGjXnbf4l7qYS4dqmvhqmxgox33oWds82NerjPi36GnpOAhs6MfIickauDnLx5tqSPf4TRknezzY16uM-Le3mrho1523-Je6mEuHapr4apsYKMd96FhKbagIer3H6LeauGjXnb'));
			halt(think_encrypt('1111111111111111112dddddddddddddddddddddddddddddd11111111111111111111111111111'));
			dump(ini_get('extension_dir'));
			require_once app()->getRootPath() . 'extend/RSA.php';

			halt(RSA::getSecretKey());
			$decrypted = RSA::decrypt(1, 2, 3);

			halt($decrypted);
			$order_wait_send = (int)Config::get('store.order_wait_send');

			halt($order_wait_send);
			$order = OrderModel::find();


			$param['id'] = 10031;
			$param['module'] = 'api';
			$param['channel'] = 'qq';
			$user = new User();
			$re = $user->login($param, $module = 'api', $by = 'oauth');
			return json($re);
//			exit();
			$arr = [
				['age' => 8,
					'name' => 'ddd8d'],
				['age' => 1,
					'name' => 'ddd1d'],
				['age' => 3,
					'name' => 'ddd3d'],
				['age' => 11,
					'name' => 'dd1dd'],
			];
			array_multisort(array_column($arr, 'age'), SORT_ASC, $arr);

			halt($arr);
			halt(sprintf("%05d", 1));
			$order = new OrderEvent();

			$re = $order->settlement();
			dump($re);
		}

		public function db()
		{

			$tables = [
				'action_log',
				'app_device_push',
				'app_device',
				'bill',
				'complaint',
				'distribution',
				'distribution_log',
				'distribution_code',
				'distribution_reward',
				'distribution_user',
				'email_history',
				'email_address',
				'feedback',
				'leave_message',
				'message',
				'queue_job',
				'pay',
				'payment_qrcode',
				'pay_offline',
				'pay_qrcode',
				'sms',
				'good',
				'good_card',
				'good_category',
				'good_click',
				'good_log',
				'good_visit',
				'store_visit',
				'order',
				'order_good',
				'order_log',
				'user_online',
				'user_balance',
				'user_balance_recharge',
				'user_balance_cash',
				'user_experience',
				'user_oauth',
				'visit',
				'visit_log',
//				'wechat_fans',
//				'wechat_media',
//				'wechat_menu',
//				'wechat_menu_media',
//				'wechat_menu_news',
//				'wechat_menu_text',
//				'wechat_news',
//				'wechat_news_item',
//				'wechat_reply',
//				'wechat_reply_media',
//				'wechat_reply_news',
//				'wechat_reply_text',
//				'wechat_template_log',
			];

			foreach ($tables as $table) {

				Db::execute('truncate q7_' . $table . ';');
			}

			echo '已经执行清空表';
		}
	}
